package loginFlash
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[SWF(width="1000",height="600",backgroundColor="#000000")]
	public class SanguoLogin extends Sprite
	{
		[Embed(source="assets/other/back.png")]
		private var pic:Class;
		[Embed(source="assets/other/background.jpg")]
		private var pic0:Class;
		[Embed(source="assets/login/image1.png")]
		private var pic1:Class;
		[Embed(source="assets/login/image2.png")]
		private var pic2:Class;
		[Embed(source="assets/login/image3.png")]
		private var pic3:Class;
		[Embed(source="assets/login/image4.png")]
		private var pic4:Class;
		[Embed(source="assets/login/image5.png")]
		private var pic5:Class;
		
		private var gen1:Bitmap,gen2:Bitmap,gen3:Bitmap,gen4:Bitmap,gen5:Bitmap,background:Bitmap,back:Bitmap;
		
		private var animates:Array;
		
		private var factor:Number = .2;
		
		private var initial:Boolean = false;
		
		private var ready:Boolean = false;
		
		public function SanguoLogin()
		{
			super();
			init();
			this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
		}
		
		public function onMouseMove(event:MouseEvent):void {
			ready = true;
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
		}
		
		public function onEnterFrame(event:Event):void {
			if(!initial) {
				for(var i:int=0;i<animates.length;i++) {
					var element:Object = animates[i];
					if(!element.init) {
						if(Math.abs(element.x - element.animate.x)<2&&Math.abs(element.y - element.animate.y)<2) {
							element.animate.x = element.x;
							element.animate.y = element.y;
							element.init = true;
							element.animate.alpha = 1;
						} else {
							element.animate.x += (element.x-  element.animate.x)*.6;
							element.animate.y += (element.y -  element.animate.y)*.6;
							element.animate.alpha += (1-element.animate.alpha)*.2;
						}
						initial = false;
						if(Math.abs(element.x - element.animate.x)<5&&Math.abs(element.y - element.animate.y)<5) {
							
						}else
						break;
					}else {
						initial = true;
					}
				}
			}else if(ready){
				var ratio:Number = (mouseX-stage.stageWidth/2)/(stage.stageWidth/2);
				for(i=0;i<animates.length;i++) {
					element = animates[i];
					var targetX:Number = element.orientation*ratio*element.offset + element.x;
					if(Math.abs(targetX - element.animate.x)<2) {
						element.animate.x = targetX;
					} else {
						element.animate.x += (targetX -  element.animate.x)*factor;
					}
				}
			}
		}
		
		public function init():void {
			gen1 = new pic1() as Bitmap;
			gen2 = new pic2() as Bitmap;
			gen3 = new pic3() as Bitmap;
			gen4 = new pic4() as Bitmap;
			gen5 = new pic5() as Bitmap;
			background = new pic0() as Bitmap;
			back = new pic() as Bitmap;
			
//			gen1.x = (stage.stageWidth - gen1.width)/2;
			gen1.y = (stage.stageHeight - gen1.height)/2;
//			gen2.x = gen1.x - gen1.width/2 + 100;
//			gen3.x = gen1.x + gen1.width/2 + 30;
//			gen4.x = gen2.x - gen2.width/2 - 20;
//			gen5.x = gen3.x + gen3.width/2 - 100;
			gen5.y = gen4.y = gen3.y = gen2.y = gen1.y;
			
			gen1.x = (stage.stageWidth - gen1.width)/2;//-gen1.width-100;
			gen2.x = -gen2.width-100;
			gen3.x = stage.stageWidth+gen3.width+100;
			gen4.x = -gen4.width-100;
			gen5.x = stage.stageWidth+gen5.width+100;
			
			gen1.y = -100;
			
			gen1.alpha = gen2.alpha = gen3.alpha = gen4.alpha = gen5.alpha = 0;			
		
			var gen1X:Number = (stage.stageWidth - gen1.width)/2;
			var gen1Y:Number = (stage.stageHeight - gen1.height)/2;
			var gen2X:Number = gen1X - gen1.width/2 + 100;
			var gen3X:Number = gen1X + gen1.width/2 + 30;
			var gen4X:Number = gen2X - gen2.width/2 - 20;
			var gen5X:Number = gen3X + gen3.width/2 - 100;
			
			var gen2Y:Number,gen3Y:Number,gen4Y:Number,gen5Y:Number; 
			gen5Y = gen4Y = gen3Y = gen2Y = gen1Y;
			
			background.x = (stage.stageWidth - background.width)/2;
			this.addChild(background);
			this.addChild(gen5);
			this.addChild(gen4);
			this.addChild(gen3);
			this.addChild(gen2);
			this.addChild(gen1);
			this.addChild(back);
			
			animates = [
				{animate:background,x:background.x,y:background.y,orientation:-1,offset:100,init:true},
				{animate:gen1,x:gen1X,y:gen1Y,orientation:-1,offset:15,init:false},
				{animate:gen2,x:gen2X,y:gen2Y,orientation:1,offset:20,init:false},
				{animate:gen3,x:gen3X,y:gen3Y,orientation:1,offset:20,init:false},
				{animate:gen4,x:gen4X,y:gen4Y,orientation:1,offset:40,init:false},
				{animate:gen5,x:gen5X,y:gen5Y,orientation:1,offset:40,init:false}
			];
		}
	}
}